package com.utils.basehelper.date;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeUtil {

	/**
	 * 获取今天是本月第几天
	 */
	public static Integer getDayOfMonth() {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.DAY_OF_MONTH);
	}

	/**
	 * 获取当前日期是本周的第几天
	 * @return
	 */
	public static Integer getDayOfWeek(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		boolean isFirstSunday = calendar.getFirstDayOfWeek() == Calendar.SUNDAY;
		int weekDay = calendar.get(Calendar.DAY_OF_WEEK);
		//若一周第一天为星期天，则-1
		if(isFirstSunday){
			weekDay = weekDay - 1;
			if(weekDay == 0){
				weekDay = 7;
			}
		}
		return weekDay;
	}

	/**
	 * 获取本月指定天数的日期
	 * 
	 * @param day
	 *            天数 例如本月是 2020-07，day设置为2，则返回日期：2020-07-02
	 */
	public static Date getMonthOfDay(int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 获取下月指定天数的日期
	 * 
	 * @param day
	 *            天数 例如本月是 2020-07，day设置为2，则返回日期：2020-08-02
	 */
	public static Date getNextMonthOfDay(int day) {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		return calendar.getTime();
	}

	/**
	 * 两个时间段是否没有交叉
	 * @param oldStartTime A时间段开始时间
	 * @param oldEndTime A时间段结束时间
	 * @param startTime B时间段开始时间
	 * @param endTime B时间段结束时间
	 * @return
	 */
	public static boolean isNoOverLap(Date oldStartTime, Date oldEndTime, Date startTime, Date endTime) {
		long oldStart = oldStartTime.getTime();
		long oldEnd = oldEndTime.getTime();
		long start = startTime.getTime();
		long end = endTime.getTime();
		return (end < oldStart || start >= oldEnd);
	}

	/**
	 * 是否在时间范围内
	 * @param startDate
	 * @param endDate
	 * @param date
	 * @return
	 */
	public static boolean isBetweenDate(Date startDate, Date endDate, Date date) {
		return date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime();
	}

	public static Date getMonthEnd(Date date) {
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	public static Date setDayOfMonth(Date date, int month, int addMonth, int day) {
		Calendar calendar = Calendar.getInstance();
		if (date != null) {
			calendar.setTime(date);
		}
		calendar.set(Calendar.HOUR_OF_DAY, 0);
		calendar.set(Calendar.MINUTE, 0);
		calendar.set(Calendar.SECOND, 0);
		calendar.set(Calendar.MILLISECOND, 0);
		calendar.set(Calendar.DAY_OF_MONTH, day);
		if (month != 0) {
			calendar.set(Calendar.MONTH, month - 1);
		}
		if (addMonth != 0) {
			calendar.add(Calendar.MONTH, addMonth);
		}
		return calendar.getTime();
	}

	public static void main(String[] args) {
		System.out.println(getDayOfMonth());
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getMonthOfDay(2)));
		System.out.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(getNextMonthOfDay(2)));
	}
}
